package com.sancorseguros.nova.selenium.pageobject;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import com.sancorseguros.nova.selenium.config.StaticSelenium;

public class EnterLossPage extends AbstractPage {

	private static final long serialVersionUID = 9191576142378273288L;

	private static final String BRANCH_AUTO = "Automotores";
	/*
	private static final String BRANCH_OTHER = "Other branch";
	*/
	
	private WebDriver driver;
	
    public EnterLossPage(WebDriver driver) throws InterruptedException {  
        super();  
        this.setDriver(driver);
        this.setTitle(StaticSelenium.TITLE_ENTER_LOSS);
	}

	
	public void doEnterLoss(String reference, String branch) throws InterruptedException {
		Actions action = new Actions(this.getDriver());
		this.getDriver().findElement(By.tagName("h1")).click();
		this.setBranchType(branch);
		action.sendKeys(Keys.TAB).sendKeys(reference).perform();
		action.sendKeys(Keys.TAB).sendKeys(Keys.TAB).sendKeys("0").perform();
	}

	
	public LossDetailPage gotoLossDetail(WebDriver driver) throws InterruptedException{
		Actions action = new Actions(this.getDriver());
		action.sendKeys(Keys.TAB).sendKeys(Keys.SPACE).perform();
		this.waitForTitle(this.getTitle(), this.getDriver().getTitle());
		return new LossDetailPage(this.getDriver());
	}
	
	public EnterLossDetailPage gotoEnterLossDetail(WebDriver driver) throws InterruptedException{
		Actions action = new Actions(this.getDriver());
		action.sendKeys(Keys.TAB).sendKeys(Keys.SPACE).perform();
		this.waitForTitle(this.getTitle(), this.getDriver().getTitle());
		return new EnterLossDetailPage(this.getDriver());
	}
	
	public void setBranchType(String branch) {
		Actions action = new Actions(driver);
		
		if (branch.equals(BRANCH_AUTO)) {
			action.sendKeys(Keys.TAB).sendKeys(Keys.SPACE)
			.sendKeys(Keys.DOWN)
			.sendKeys(Keys.DOWN)
			.sendKeys(Keys.DOWN)
			.sendKeys(Keys.DOWN)
			.sendKeys(Keys.DOWN)
			.sendKeys(Keys.ENTER).perform();
		} else {
			//TODO otra cosa
		}
	}

	@Override
	public void validatePage() {
		assertTrue(StaticSelenium.INVALID_PAGE, this.verifyPage(this.getDriver(), StaticSelenium.TITLE_LOSS_MANAGEMENT));
	}

	public WebDriver getDriver() {
		return driver;
	}

	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}
	
}
